//Situation.cpp
/*
Situation class
Created by: Taryn Green & Chris Groce
Updated: 10/10/2013
*/

#include "cstdlib"
#include "Situation.h"
using namespace std;

Situation::Situation()
{	
	srand (time(NULL));		// seed rand
	int swarmSize = 5;
	float roomSize=7;
	Player * tmp= new Player(roomSize);
	Player * tmp2= new Player(roomSize);
	opponent = *tmp;
	player = *tmp2;
	Room * r= new Room(roomSize);
	room = *r;
	for (int i = 0; i < swarmSize; i++){ 
	  Drone * d = new Drone(roomSize);
	  swarm.push_back(*d);
	}
	player.setXPos(-1.0f);
	opponent.setXPos(1.0f);

	for (int i = 0; i < swarmSize; i++){
	  swarm[i].randomizePos();
	}
}


Player Situation::getPlayer()
{
	return player;
}

Player Situation::getOpponent()
{
	return opponent;
}

Drone Situation::getDrone(int swarm_id)
{
	return swarm[swarm_id];
}

void Situation::setOpponent(Player m_opponent)
{
	opponent=m_opponent;
}

void Situation::setPlayer(Player m_player)
{
	player=m_player;
}

void Situation::setRoom(Room m_room)
{
	room = m_room;
}

void Situation::setDrone(Drone m_drone, int swarm_id)
{
	swarm[swarm_id] = m_drone ;
}

Room Situation::getRoom()
{
	return room;
}

int Situation::getSwarmSize()
{	
	return swarm.size();
}

void Situation::advancePlayerLeft()
{
	player.incrementLeftSpeed();
}
void Situation::advancePlayerRight()
{
	player.decrementLeftSpeed();
}
void Situation::advanceShots()
{
	int inRoom;
	size_t i;
	for(i = 0; i < bullets.size(); i++)
	{
		inRoom = bullets[i].updatePositions();
		if(inRoom == 0)
		{
		  bullets.erase(bullets.begin()+i);
		  i--;
		}
		else
		  for(int j = 0; j < swarm.size(); j++)
		  {
		    if(detectCollision(bullets[i], swarm[j]))
		    {
		      bullets.erase(bullets.begin()+i);
		      swarm.erase(swarm.begin()+j);
		      j--;
		      i--;
		      break;    
		    }
	  	  }
		
	}
}

int Situation::detectCollision(Shot bullet, Drone enemy)
{
  if(bullet.getXPos() + bullet.getSize()/2 > enemy.getXPos() - enemy.getSize()/2
  && bullet.getXPos() + bullet.getSize()/2 < enemy.getXPos() + enemy.getSize()/2
  || bullet.getXPos() - bullet.getSize()/2 < enemy.getXPos() + enemy.getSize()/2
  && bullet.getXPos() - bullet.getSize()/2 > enemy.getXPos() - enemy.getSize()/2)
    if(bullet.getZPos() + bullet.getSize()/2 > enemy.getZPos() - enemy.getSize()/2
    && bullet.getZPos() + bullet.getSize()/2 < enemy.getZPos() + enemy.getSize()/2
    || bullet.getZPos() - bullet.getSize()/2 < enemy.getZPos() + enemy.getSize()/2
    && bullet.getZPos() - bullet.getSize()/2 > enemy.getZPos() - enemy.getSize()/2)
      return 1;
  return 0;
}


int Situation::getNumShots()
{
	return bullets.size();
}
Shot Situation::getShot(int index)
{
	return bullets[index];
}
void Situation::shoot()
{
	Shot newBullet;
	newBullet.setXPos(player.getXPos());
	newBullet.setYPos(player.getYPos());
	newBullet.setZPos(player.getZPos());
	newBullet.setHorFacing(player.getHorFacing());
	newBullet.setVertFacing(player.getVertFacing());
	bullets.push_back(newBullet);
}
void Situation::turnPlayerRight()
{
	player.turnRight();
}
void Situation::turnPlayerLeft()
{
	player.turnLeft();
}
void Situation::turnPlayerUp()
{
	player.turnUp();
}
void Situation::turnPlayerDown()
{
	player.turnDown();
}
void Situation::advancePlayerForward()
{
	player.incrementForwardSpeed();
}
void Situation::advancePlayerBackward()
{
	player.decrementForwardSpeed();
}
void Situation::advancePlayerUp()
{
	player.incrementUpSpeed();
}
void Situation::advancePlayerDown()
{
	player.decrementUpSpeed();
}

void Situation::updatePlayerPositions()
{
	player.updatePositions();
	opponent.updatePositions();
}
void Situation::resetPlayerSpeeds()
{
	player.resetSpeeds();
	opponent.resetSpeeds();
}
